package cn.rongdao.jrkabao.ui.information.adapter.baseadapter;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/*
*
 * Created by Wang on 2017/2/17.
 * RecycleView的基类adapter,注意加泛型(该泛型是LIst<>数据里面的泛型)
 * */
public abstract class BaseRecycleAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
        //存储监听回调
        private SparseArray<ItemClickListener> onClickListeners;

        private List<T> dataList;

    public BaseRecycleAdapter() {
        if(dataList == null){
            dataList = new ArrayList<T>();
        }
        if(onClickListeners == null){
            onClickListeners = new SparseArray<>();
        }
    }

    public List<T> getDataList() {
        if(dataList == null){
            dataList = new ArrayList<>();
        }
        return dataList;
    }

    public interface ItemClickListener {
            void onItemClicked(View view, int position);
        }
        public void setRefreshData(List<T> data){
            dataList.clear();
            dataList.addAll(data);
            notifyDataSetChanged();
         }
    public void setLoadMoveData(List<T> data){
        dataList.addAll(data);
        notifyDataSetChanged();
    }
        public void setOnItemClickListener(int viewId,ItemClickListener listener) {
            ItemClickListener listener_ = onClickListeners.get(viewId);
            if(listener_ == null){
                onClickListeners.put(viewId,listener);
            }
        }
/*
*
         * 获取列表控件的视图id(由子类负责完成)
         * @param viewType
         * @return
*/


        public abstract int getLayoutId(int viewType);
        //更新itemView视图(由子类负责完成)
        public abstract void convert(BaseViewHolder holder, T t, int position);

        public T getItem(final int position){
            if(dataList == null)
                return null;
            return dataList.get(position);
        }

        @Override
        public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            int layoutId = getLayoutId(viewType);
            BaseViewHolder viewHolder = BaseViewHolder.getViewHolder(parent, layoutId);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(BaseViewHolder holder, final int position) {
            T itemModel = dataList.get(position);
            convert(holder, itemModel, position);//更新itemView视图
            //设置点击监听
            for (int i = 0; i < onClickListeners.size(); ++i){
                int id = onClickListeners.keyAt(i);
                View view = holder.getView(id);
                if(view == null)
                    continue;
                final ItemClickListener listener = onClickListeners.get(id);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(listener != null){
                            listener.onItemClicked(v,position);
                        }
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            if (dataList == null)
                return 0;
            return dataList.size();
        }

        public void destroyAdapter(){
            if(dataList != null)
                dataList.clear();
            dataList = null;
        }
    }



